package com.atguigu.commonutils;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

/**
 * 项目中我们会将响应封装为json格式,会将数据格式统一，统一返回消息没有具体的格式，但是有响应是否成功、状态码、响应消息、数据
 * {
 *     "success":布尔//响应是否成功
 *     "code":状态码
 *     "message":响应消息
 *     "data":hashMap
 * }
 */
@Data
public class Result {
    @ApiModelProperty(value = "响应是否成功")
    private Boolean success;
    @ApiModelProperty(value = "状态码")
    private Integer code;
    @ApiModelProperty(value = "响应消息")
    private String message;
    @ApiModelProperty(value = "返回数据")
    private Map<String,Object> data=new HashMap<>();

    //私有构造器
    private Result(){}

    //成功静态方法
    public static Result ok(){
        Result result=new Result();
        result.setCode(ResultCode.SUCCESS);
        result.setMessage("成功");
        result.setSuccess(true);
        return result;
    }

    //失败静态方法
    public static Result error(){
        Result result=new Result();
        result.setCode(ResultCode.ERROR);
        result.setMessage("失败");
        result.setSuccess(false);
        return result;
    }

    public Result success(Boolean success){
        this.setSuccess(success);
        return this;
    }
    public Result code(Integer code){
        this.setCode(code);
        return this;
    }
    public Result message(String message){
        this.setMessage(message);
        return this;
    }
    public Result data(String key,Object value){
        this.data.put(key,value);
        return this;
    }
    public Result data(Map<String,Object> map){
        this.setData(map);
        return this;
    }
}
